//package org.batatasframework.web.method.resolver;
//
//import java.util.HashMap;
//import java.util.Map;
//
//import org.batatasframework.authenticated.AuthenticatedService;
//import org.batatasframework.platform.PlatformManager;
//import org.batatasframework.web.model.AbstractModel;
//import org.batatasframework.web.model.DialogModel;
//import org.batatasframework.web.model.PanelModel;
//import org.batatasframework.web.template.TemplateManager;
//import org.springframework.core.MethodParameter;
//import org.springframework.ui.ModelMap;
//import org.springframework.util.Assert;
//import org.springframework.web.bind.support.WebDataBinderFactory;
//import org.springframework.web.context.request.NativeWebRequest;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.method.support.ModelAndViewContainer;
//import org.springframework.web.servlet.HandlerMapping;
//import org.springframework.web.servlet.View;
//
//import cn.bonoon.kernel.security.LogonUser;
//
///**
// * 可以通过参数，直接生成几种通用的界面
// * @author jackson
// *
// */
//public class LayoutModelArgumentResolver extends AbstractArgumentResolver{
//
//	private String keyVariableName = "key";
//	private String modelVariableName = "model";
//	
//	private PlatformManager platformManager;
//	private TemplateManager templateManager;
//	private AuthenticatedService authenticatedService;
//
//	@Override
//	public void afterPropertiesSet() throws Exception {
//		platformManager = beanFactory.getBean(PlatformManager.class);
//		templateManager = beanFactory.getBean(TemplateManager.class);
//		authenticatedService = beanFactory.getBean(AuthenticatedService.class);
//	}
//
//	@Override
//	public boolean supportsParameter(MethodParameter parameter) {
//		Class<?> paramType = parameter.getParameterType();
//		return (PanelModel.class.isAssignableFrom(paramType) ||
////				GridModel.class.isAssignableFrom(paramType) ||
//				DialogModel.class.isAssignableFrom(paramType));
//	}
//
//	@Override
//	@SuppressWarnings("unchecked")
//	public final Object resolveArgument(
//			MethodParameter parameter, ModelAndViewContainer mavContainer,
//			NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
//			throws Exception {
//		
//		Map<String, String> uriTemplateVars =
//				(Map<String, String>) webRequest.getAttribute(
//						HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
//		Assert.notNull(uriTemplateVars, "访问路径不符合规则！");
//		
//		String keyValue = uriTemplateVars.get(keyVariableName);
//		Assert.hasText(keyValue, "访问路径不符合规则！");
//		handleResolvedKeyValue(keyValue, keyVariableName, parameter, mavContainer, webRequest);
//		
//		Class<?> paramType = parameter.getParameterType();
//		ModelMap modelMap = mavContainer.getModel();
//		LogonUser user = getUser();
//		AbstractModel resultModel = null;
//		
//		if(PanelModel.class.isAssignableFrom(paramType)){
//			resultModel = new PanelModel(keyValue, user, modelMap, platformManager, templateManager, authenticatedService);
//		}else if(DialogModel.class.isAssignableFrom(paramType)){
//			resultModel = new DialogModel(keyValue, user, modelMap, platformManager, templateManager, authenticatedService);
//		}
//
//		if(null != resultModel){
//			return handleResulvedValue(modelMap, modelVariableName, resultModel);
//		}
//		
//		return unsupported(parameter);
//	}
//	
//	private AbstractModel handleResulvedValue(ModelMap modelMap, String modelKey, AbstractModel modelValue){
//		modelMap.addAttribute(modelKey, modelValue);
//		return modelValue;
//	}
//
//	@SuppressWarnings("unchecked")
//	private void handleResolvedKeyValue(Object arg, String name, MethodParameter parameter,
//			ModelAndViewContainer mavContainer, NativeWebRequest request) {
//		
//		String key = View.PATH_VARIABLES;
//		int scope = RequestAttributes.SCOPE_REQUEST;
//		Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(key, scope);
//		if (pathVars == null) {
//			pathVars = new HashMap<String, Object>();
//			request.setAttribute(key, pathVars, scope);
//		}
//		pathVars.put(name, arg);
//	}
//
//}
